Skip to content

Add linting for logging function#740

Open
himax16 wants to merge 5 commits intocanonical:mainfrom
himax16:add-logging-linting
Open

Add linting for logging function#740
himax16 wants to merge 5 commits intocanonical:mainfrom
himax16:add-logging-linting

Conversation

@himax16
Copy link
Copy Markdown
Member

@himax16 himax16 commented Apr 6, 2026

Add automatic linting for the logging function based on the LOG and G rules in ruff.

This will standardize the logging across the Python codebase.

For references:
Error logging pattern

try:
    raise ValueError("Bad thing") from Exception("original cause")
except ValueError as e:
    LOG.debug("Error with %s       : %s", r"%s", e)
    LOG.debug("Error with %s       : %r", r"%r", e)
    LOG.debug("Error with e        : %s", e)
    LOG.debug(e, exc_info=True)
    LOG.exception("Caught an exception")

gives

07:28:03,370 __main__ DEBUG   Error with %s       : Bad thing
07:28:03,370 __main__ DEBUG   Error with %r       : ValueError('Bad thing')
07:28:03,370 __main__ DEBUG   Error with e        : Bad thing
07:28:03,370 __main__ DEBUG   Bad thing
Exception: original cause

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubuntu/log_testing.py", line 33, in main
    raise ValueError("Bad thing") from Exception("original cause")
ValueError: Bad thing
07:28:03,370 __main__ ERROR   Caught an exception
Exception: original cause

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubuntu/log_testing.py", line 33, in main
    raise ValueError("Bad thing") from Exception("original cause")
ValueError: Bad thing

subprocess error logging pattern

try:
    subprocess.run(
        ["juju", "show-controller", "nonexistent"],
        check=True,
        capture_output=True,
        text=True,
    )
except subprocess.CalledProcessError as e:
    LOG.exception("Caught a CalledProcessError")
    LOG.debug("e      : %s", e)
    LOG.debug("%s     : %s", r"%s", str(e))
    LOG.debug("%s     : %r", r"%r", e)
    LOG.debug("Output : %s", e.output)
    LOG.debug("Stderr : %s", e.stderr)

gives

07:34:06,433 __main__ ERROR   Caught a CalledProcessError
Traceback (most recent call last):
  File "/home/ubuntu/log_testing.py", line 45, in main
    subprocess.run(
  File "/usr/lib/python3.12/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['juju', 'show-controller', 'nonexistent']' returned non-zero exit status 1.
07:34:06,434 __main__ DEBUG   e      : Command '['juju', 'show-controller', 'nonexistent']' returned non-zero exit status 1.
07:34:06,434 __main__ DEBUG   %s     : Command '['juju', 'show-controller', 'nonexistent']' returned non-zero exit status 1.
07:34:06,434 __main__ DEBUG   %r     : CalledProcessError(1, ['juju', 'show-controller', 'nonexistent'])
07:34:06,434 __main__ DEBUG   Output : {}

07:34:06,434 __main__ DEBUG   Stderr : ERROR controller nonexistent not found

gboutry
gboutry previously approved these changes Apr 6, 2026
Copy link
Copy Markdown
Collaborator

@gboutry gboutry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this, and enabling the appropriate linter plugins

This will still need rebase on main, but good job

"S", # bandit
"I", # isort
"CPY", # flake8-copyright
"LOG", # flake8-logging
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good plugins to add.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enables Ruff’s flake8-logging (LOG) and flake8-logging-format (G) rules to standardize Python logging usage (consistent logger objects and lazy formatting), and updates the codebase accordingly.

Changes:

  • Enable Ruff LOG/G lint rules in pyproject.toml.
  • Update many Python modules/tests to use module-level LOG = logging.getLogger(__name__) and %-style lazy formatting instead of f-strings / direct logging.* calls.
  • Minor tox command cleanup (formatting + allowing {posargs} to override default paths in fmt/pep8 envs).

Reviewed changes

Copilot reviewed 68 out of 68 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sunbeam-python/pyproject.toml Enables Ruff LOG and G rule families.
sunbeam-python/tox.ini Normalizes command formatting; allows posargs for fmt/pep8 target paths.
sunbeam-python/tests/functional/local/utils.py Introduces module LOG and updates info logs to use it.
sunbeam-python/tests/functional/local/test_sriov.py Uses module LOG and lazy formatting.
sunbeam-python/tests/functional/local/test_network_node.py Uses module LOG and lazy formatting.
sunbeam-python/tests/functional/local/test_dpdk.py Uses module LOG; adjusts debug formatting specifiers.
sunbeam-python/sunbeam/utils.py Replaces eager logging with structured/lazy logging.
sunbeam-python/sunbeam/storage/steps.py Converts f-string logs to lazy formatting; updates warning/error logging.
sunbeam-python/sunbeam/storage/service.py Converts f-string logs to lazy formatting; improves exception logging formatting.
sunbeam-python/sunbeam/storage/manager.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/storage/base.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/steps/vault.py Converts f-string logs to lazy formatting; improves exception formatting.
sunbeam-python/sunbeam/steps/upgrades/intra_channel.py Converts f-string logs to lazy formatting; improves timeout warnings.
sunbeam-python/sunbeam/steps/upgrades/inter_channel.py Converts f-string logs to lazy formatting; minor wording fix (“MySQL”).
sunbeam-python/sunbeam/steps/sync_feature_gates.py Converts warning to lazy formatting; clarifies messages.
sunbeam-python/sunbeam/steps/openstack.py Converts f-string logs to lazy formatting; improves timeout/invalid input warnings.
sunbeam-python/sunbeam/steps/mysql.py Converts f-string logs to lazy formatting; adjusts messages/casing.
sunbeam-python/sunbeam/steps/microovn.py Converts f-string logs to lazy formatting; improves timeout warning.
sunbeam-python/sunbeam/steps/microceph.py Converts f-string logs to lazy formatting; tweaks message punctuation.
sunbeam-python/sunbeam/steps/k8s.py Converts f-string logs to lazy formatting; improves exception logging.
sunbeam-python/sunbeam/steps/juju.py Converts f-string logs to lazy formatting; improves subprocess stderr logging.
sunbeam-python/sunbeam/steps/hypervisor.py Removes traceback usage; uses logger exception context instead; converts logs.
sunbeam-python/sunbeam/steps/configure.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/steps/clusterd.py Avoids logging raw objects directly; adds context to cluster-related logs.
sunbeam-python/sunbeam/steps/cluster_status.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/steps/cinder_volume.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/provider/maas/steps.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/provider/maas/commands.py Converts f-string logs to lazy formatting; improves error wording.
sunbeam-python/sunbeam/provider/local/steps.py Converts f-string + logging.* calls to module LOG and lazy formatting.
sunbeam-python/sunbeam/provider/local/deployment.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/provider/local/commands.py Converts f-string logs to lazy formatting; improves exception logging.
sunbeam-python/sunbeam/provider/common/multiregion.py Switches from logging.debug to module LOG.debug.
sunbeam-python/sunbeam/log.py Converts f-string debug log to lazy formatting.
sunbeam-python/sunbeam/hooks.py Converts f-string logs to lazy formatting; switches to LOG.info in hooks.
sunbeam-python/sunbeam/features/vault/feature.py Converts f-string logs to lazy formatting; avoids logging secrets; improves wording.
sunbeam-python/sunbeam/features/validation/feature.py Converts f-string logs to lazy formatting; minor punctuation consistency.
sunbeam-python/sunbeam/features/tls/common.py Converts f-string logs to lazy formatting across action helpers.
sunbeam-python/sunbeam/features/telemetry/feature.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/shared_filesystem/manila_data.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/observability/feature.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/maintenance/checks.py Converts f-string logs to lazy formatting; improves debug context.
sunbeam-python/sunbeam/features/ldap/feature.py Improves timeout warnings; converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/interface/v1/openstack.py Converts f-string logs to lazy formatting; improves timeout logs.
sunbeam-python/sunbeam/features/interface/v1/base.py Converts f-string logs to lazy formatting; improves warnings with context.
sunbeam-python/sunbeam/features/interface/utils.py Converts “log raw exception” to contextual lazy formatting.
sunbeam-python/sunbeam/features/instance_recovery/consul.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/dns/feature.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/features/caas/feature.py Converts f-string logs to lazy formatting; improves messages.
sunbeam-python/sunbeam/features/baremetal/steps.py Converts f-string logs to lazy formatting; adjusts exception logging.
sunbeam-python/sunbeam/feature_manager.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/feature_gates.py Converts f-string logs to lazy formatting; improves gated-feature debug message.
sunbeam-python/sunbeam/core/watcher.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/core/terraform.py Converts f-string logs to lazy formatting; improves exception logging for subprocess failures.
sunbeam-python/sunbeam/core/steps.py Converts f-string logs to lazy formatting; improves debug messages for k8s annotations/pools.
sunbeam-python/sunbeam/core/questions.py Converts f-string logs to lazy formatting; improves error repr logging.
sunbeam-python/sunbeam/core/openstack_api.py Converts f-string info logs to lazy formatting.
sunbeam-python/sunbeam/core/k8s.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/core/juju.py Converts f-string logs to lazy formatting; improves track-compare error logging.
sunbeam-python/sunbeam/core/deployments.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/core/deployment.py Converts f-string logs to lazy formatting; adjusts log levels/messages.
sunbeam-python/sunbeam/core/common.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/core/checks.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/commands/refresh.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/commands/proxy.py Fixes click.echo error message formatting; converts debug logs to lazy formatting.
sunbeam-python/sunbeam/commands/manifest.py Converts f-string logs to lazy formatting; switches to LOG.exception on failure.
sunbeam-python/sunbeam/commands/launch.py Improves exception logging (use LOG.exception) and avoids f-string logging.
sunbeam-python/sunbeam/commands/generate_cloud_config.py Converts f-string logs to lazy formatting.
sunbeam-python/sunbeam/commands/configure.py Converts f-string logs to lazy formatting; improves exception logging consistency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sunbeam-python/sunbeam/steps/mysql.py Outdated
Comment thread sunbeam-python/sunbeam/steps/juju.py
Comment thread sunbeam-python/sunbeam/features/baremetal/steps.py Outdated
@himax16 himax16 force-pushed the add-logging-linting branch from c48e22d to 641ab5d Compare April 24, 2026 03:54
@himax16 himax16 requested review from Copilot and gboutry April 24, 2026 03:55
@himax16 himax16 marked this pull request as ready for review April 24, 2026 03:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 76 out of 76 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sunbeam-python/sunbeam/steps/juju.py
Comment thread sunbeam-python/sunbeam/steps/juju.py Outdated
Comment thread sunbeam-python/sunbeam/features/interface/v1/openstack.py Outdated
Comment thread tools/add_charm_revisions.py
Comment thread sunbeam-python/pyproject.toml
himax16 added 5 commits April 24, 2026 06:46
Signed-off-by: Himawan Winarto <himawan.winarto@canonical.com>
Refactor logging calls on the files that break the linter

Signed-off-by: Himawan Winarto <himawan.winarto@canonical.com>
Signed-off-by: Himawan Winarto <himawan.winarto@canonical.com>
Signed-off-by: Himawan Winarto <himawan.winarto@canonical.com>
Signed-off-by: Himawan Winarto <himawan.winarto@canonical.com>
@himax16 himax16 force-pushed the add-logging-linting branch from 641ab5d to f0f6188 Compare April 24, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants